package com.lagou.controller;

import com.lagou.domain.Course;
import com.lagou.domain.CourseLesson;
import com.lagou.domain.CourseSection;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Struct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    /**
     * 章节及课时内容查询
     * @param courseId
     * @return
     */
    @RequestMapping("/findSectionAndLessonByCourseId")
    public ResponseResult findSectionAndLessonByCourseId(Integer courseId) {

        List<CourseSection> list = courseContentService.findSectionAndLessonByCourseId(courseId);

        return new ResponseResult(true,200,"章节及课时内容查询成功",list);

    }

    /**
     * 课程查询
     * @param courseId
     * @return
     */
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId) {
        Course course = courseContentService.findCourseByCourseId(courseId);
        return new ResponseResult(true,200,"课程查询成功",course);
    }

    /**
     * 新增和更新章节信息
     * @param courseSection
     * @return
     */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection) {

        if (courseSection.getId() == null) {
            courseContentService.saveSection(courseSection);
            return new ResponseResult(true,200,"添加成功",null);
        } else {
            courseContentService.updateSection(courseSection);
            return new ResponseResult(true,200,"更新成功",null);
        }
    }

    /**
     * 更新章节状态
     * @return
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(int id, int status) {

        courseContentService.updateSectionStatus(id, status);

        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true,200,"状态更新成功",map);
    }

    /**
     * 保存 & 修改课时
     * @param courseLesson
     * @return
     */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson courseLesson) {
        if (courseLesson.getId() == null) {
            courseContentService.saveLesson(courseLesson);
            return new ResponseResult(true,200,"添加成功",null);
        } else {
            courseContentService.updateLesson(courseLesson);
            return new ResponseResult(true,200,"更新成功",null);
        }

    }
}
